package com.itheima.mall.service.sms;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import com.itheima.mall.api.pms.CouponServiceApi;
import com.itheima.mall.api.sms.CouponProductCategoryRelationApi;
import com.itheima.mall.api.sms.CouponProductRelationApi;
import com.itheima.mall.pojo.sms.Coupon;
import com.itheima.mall.pojo.sms.CouponProductCategoryRelation;

import com.itheima.mall.pojo.sms.SmsCouponProductRelation;
import com.itheima.mall.vo.PageResult;
import com.itheima.mall.vo.sms.CouponVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CouponService {
    @DubboReference
    private CouponServiceApi couponServiceApi;
    @DubboReference
    private CouponProductCategoryRelationApi couponProductCategoryRelationApi;
    @DubboReference
    private CouponProductRelationApi couponProductRelationApi;


    //添加优惠券
    public void addCoupon(CouponVo couponVo) {
        couponServiceApi.addCoupon(couponVo);
    }

    //删除优惠券
    public void deleteCoupon(Integer id) {
        couponServiceApi.deleteCoupon(id);
    }
    //根据优惠券名称和类型分页获取优惠券列表
    public PageResult<Coupon> getCoupon(String name, Integer pageNum, Integer pageSize, Integer type) {
        PageResult<Coupon> pageResult=couponServiceApi.getCoupon(name,pageNum,pageSize,type);
        return pageResult;

    }
    //修改优惠券
    public void upadateCoupon(CouponVo couponVo, Integer id) {
        Long cid = Convert.toLong(id);
        couponServiceApi.upadateCoupon(couponVo,cid);
    }

    //获取单个优惠券的详细信息
    public CouponVo  selectCoupon(Integer id) {
        Long cid = Convert.toLong(id);
        Coupon coupon= couponServiceApi.selectCoupon(cid);
        List<CouponProductCategoryRelation>Cpcr=couponProductCategoryRelationApi.selectCoupon(cid);
        List<SmsCouponProductRelation>cpr=couponProductRelationApi.selectCoupon(cid);
        CouponVo couponVo=new CouponVo();
        BeanUtil.copyProperties(coupon,couponVo);
        couponVo.setProductCategoryRelationList(Cpcr);
        couponVo.setProductRelationList(cpr);
        return couponVo;
    }
}
